package com.qy139.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qy139.entity.CardInfo;
import com.qy139.entity.RoomOrder;

import com.qy139.order.service.IRoomOrderService;
import com.qy139.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hyh
 * @since 2021-12-01
 */
@RestController
@RequestMapping("/order")
public class RoomOrderController {
    private static byte[] lock = new byte[0];
    // 位数，默认是8位
    private final static long w = 100000000;

    @Resource
    private IRoomOrderService service;
    @RequestMapping("/getAll")
    public Result getAll(Page page, RoomOrder order){
        QueryWrapper<RoomOrder> queryWrapper = new QueryWrapper();
        // 模糊查询
        queryWrapper.like("cardnum",order.getCardnum());
        queryWrapper.like("store_id",order.getStoreId());
        Page page1 = service.page(page,queryWrapper);
        return new Result(200,"操作成功",page1);
    }

    @GetMapping("/getAllSeat")
    public Result getAllSeat(RoomOrder order){
        Integer id = order.getRoomId();
        Integer storeId = order.getStoreId();

        System.out.println(id+"1111111111111111111111111111111111111111111");
        System.out.println(storeId+"22222222222222222222222222222222222222222");
        List<Map> allSeat = service.getAllSeat(id,storeId);
        return new Result(200,"操作成功",allSeat);
    }
    @GetMapping("/getAllRoom/{id}")
    public Result getAllRoom(@PathVariable Integer id){
        System.out.println(id);
        List<Map> allSeat = service.getAllRoom(id);
        System.out.println(allSeat);
        return new Result(200,"操作成功",allSeat);
    }


    @DeleteMapping("/del/{id}")
    public Result Del(@PathVariable Integer id){
        boolean b = service.removeById(id);
        return new Result(200,"删除成功",b);
    }
    @PutMapping("/upd")
    public Result update(RoomOrder order){
        return new Result(service.saveOrUpdate(order));
    }

    @GetMapping("/selectTime")
    public Result selectTime(RoomOrder order){
        System.out.println(order+"1111111111111111111111111111111111111111111111111111111111");
        List<Map> maps = service.selectTime(order);
        return new Result(200,"操作成功",maps);
    }

    @PutMapping("/add")
    public Result add(RoomOrder order){
        String id = createID();
        order.setOrderId(id);
        System.out.println(order);
        return new Result(service.saveOrUpdate(order));
    }




    @GetMapping("/getCard/{cardnum}")
    public Result getCard(@PathVariable Integer cardnum){
        Map card = service.getCard(cardnum);
        System.out.println(card);

        return new Result(200,"操作成功",card);
    }


        public static String createID() {
            long r = 0;
            synchronized (lock) {
                r = (long) ((Math.random() + 1) * w);
            }

            return System.currentTimeMillis() + String.valueOf(r).substring(1);
        }





}

